/*
 * Created by JFormDesigner on Wed Jul 03 17:10:56 CST 2024
 */

package scct.yzx.ui;

import java.awt.event.*;

import scct.yzx.dao.ChatRecordDao;
import scct.yzx.dao.ConversationDao;
import scct.yzx.dao.UserDao;
import scct.yzx.model.ChatRecord;
import scct.yzx.model.Conversation;
import scct.yzx.model.User;
import scct.yzx.utils.Tools;

import java.awt.*;
import java.util.List;
import javax.swing.*;

/**
 * @author lenovo
 */
public class DlgChatRecordUpdate extends JDialog {
    int id;
    public DlgChatRecordUpdate(Window owner, int id) {
        super(owner);
        this.id=id;
        initComponents();
        initView();
    }

    private void initView() {

        UserDao userDao = new UserDao();
        List<User> allUser = userDao.getAllUser();
        for (User user:allUser) {
            senderBox.addItem(user.getUsername());
        }
        ConversationDao conversationDao=new ConversationDao();
        List<Conversation> allConversation=conversationDao.getAllConversation();
        for (Conversation conversation:allConversation){
            receiverBox.addItem(conversation.getContactname());
        }
        ChatRecordDao chatRecordDao=new ChatRecordDao();
        List<ChatRecord> chatRecords = chatRecordDao.getAllChatRecord();
        for (ChatRecord chatRecord:chatRecords){
            converidBox.addItem(chatRecord.getId());
        }
        ChatRecord chatRecord=chatRecordDao.getChatRecordId(id);
        msgTypeBox.addItem(chatRecord.getMsgtype());
        txtContent.setText(chatRecord.getContent());


    }

    private void btnUpdate(ActionEvent e) {
        // TODO add your code here
        int conversation=converidBox.getItemCount();
        String sender= (String) senderBox.getSelectedItem();
        String receiver= (String) receiverBox.getSelectedItem();
        String msgType= (String) msgTypeBox.getSelectedItem();
        String content=txtContent.getText();
        String sendtime= Tools.getCreatetime();
        ChatRecordDao chatRecordDao=new ChatRecordDao();
        if (chatRecordDao.updateChatRecord(id,conversation,sender,receiver,msgType,content,sendtime)){
            JOptionPane.showMessageDialog(this,"修改成功");

        }
        this.dispose();
    }

    private void btnCancel(ActionEvent e) {
        // TODO add your code here
        this.dispose();
    }




    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        // Generated using JFormDesigner Evaluation license - yzx
        label1 = new JLabel();
        converidBox = new JComboBox();
        label2 = new JLabel();
        senderBox = new JComboBox();
        label3 = new JLabel();
        receiverBox = new JComboBox();
        label4 = new JLabel();
        msgTypeBox = new JComboBox<>();
        label5 = new JLabel();
        txtContent = new JTextField();
        btnUpdate = new JButton();
        btnCancel = new JButton();

        //======== this ========
        setTitle("\u4fee\u6539\u804a\u5929\u8bb0\u5f55");
        var contentPane = getContentPane();
        contentPane.setLayout(null);

        //---- label1 ----
        label1.setText("\u4f1a\u8bddid");
        contentPane.add(label1);
        label1.setBounds(new Rectangle(new Point(70, 27), label1.getPreferredSize()));
        contentPane.add(converidBox);
        converidBox.setBounds(130, 20, 125, converidBox.getPreferredSize().height);

        //---- label2 ----
        label2.setText("\u53d1\u9001\u8005");
        contentPane.add(label2);
        label2.setBounds(new Rectangle(new Point(70, 65), label2.getPreferredSize()));
        contentPane.add(senderBox);
        senderBox.setBounds(130, 60, 125, senderBox.getPreferredSize().height);

        //---- label3 ----
        label3.setText("\u63a5\u6536\u8005");
        contentPane.add(label3);
        label3.setBounds(new Rectangle(new Point(70, 105), label3.getPreferredSize()));
        contentPane.add(receiverBox);
        receiverBox.setBounds(130, 100, 125, receiverBox.getPreferredSize().height);

        //---- label4 ----
        label4.setText("\u6d88\u606f\u79cd\u7c7b");
        contentPane.add(label4);
        label4.setBounds(new Rectangle(new Point(70, 150), label4.getPreferredSize()));

        //---- msgTypeBox ----
        msgTypeBox.setModel(new DefaultComboBoxModel<>(new String[] {
            "\u56fe\u7247",
            "\u6587\u6863",
            "\u89c6\u9891",
            "\u6587\u5b57"
        }));
        contentPane.add(msgTypeBox);
        msgTypeBox.setBounds(130, 140, 125, msgTypeBox.getPreferredSize().height);

        //---- label5 ----
        label5.setText("\u6d88\u606f\u5185\u5bb9");
        contentPane.add(label5);
        label5.setBounds(new Rectangle(new Point(70, 190), label5.getPreferredSize()));
        contentPane.add(txtContent);
        txtContent.setBounds(130, 185, 125, txtContent.getPreferredSize().height);

        //---- btnUpdate ----
        btnUpdate.setText("\u4fee\u6539");
        btnUpdate.addActionListener(e -> btnUpdate(e));
        contentPane.add(btnUpdate);
        btnUpdate.setBounds(new Rectangle(new Point(65, 245), btnUpdate.getPreferredSize()));

        //---- btnCancel ----
        btnCancel.setText("\u53d6\u6d88");
        btnCancel.addActionListener(e -> btnCancel(e));
        contentPane.add(btnCancel);
        btnCancel.setBounds(new Rectangle(new Point(195, 245), btnCancel.getPreferredSize()));

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        setSize(400, 365);
        setLocationRelativeTo(null);
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    // Generated using JFormDesigner Evaluation license - yzx
    private JLabel label1;
    private JComboBox converidBox;
    private JLabel label2;
    private JComboBox senderBox;
    private JLabel label3;
    private JComboBox receiverBox;
    private JLabel label4;
    private JComboBox<String> msgTypeBox;
    private JLabel label5;
    private JTextField txtContent;
    private JButton btnUpdate;
    private JButton btnCancel;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
